package user;
import book.Library;
import utils.Constant;
import utils.PermissionException;

import java.util.concurrent.Callable;

public class ProxyUser {

    private User realUser;

    public ProxyUser(User realUser) {
        this.realUser = realUser;
    }

    public User getRealUser() {
        return realUser;
    }
    //检测是否为Admin用户
    public void checkRealUserWhetherAdminUser(String exceptionMessage){
        if(!(realUser instanceof Administrator)){
            throw new PermissionException(exceptionMessage);
        }
    }
    public void checkRealUserWhetherNormalUser(String exceptionMessage){
        if(!(realUser instanceof NormalUser)){
            throw new PermissionException(exceptionMessage);
        }
    }

    public void handleOperation(int choice){
        if(realUser instanceof Administrator){
             switch(choice){
                case Constant.SEARCH_BOOK:
                   searchBook();break;
                case Constant.DISPLAY_BOOK:
                    displayBook();break;
                case Constant.SYSTEM_EXIT:
                  systemExit();break;
                case Constant.SHELF_BOOK:
                   add();break;
                case Constant.UPDATA_BOOK:
                   updataBook();break;
                case Constant.DELETE_BOOK:
                   removeBook();break;
                case Constant.BORROWED_COUNT:
                   borrowedCount();break;
                case Constant.INGENERATE_BOOK:
                    generateBook();
                    break;
                case Constant.CHEAK_INVENTORY_STATUS:
                    checkInventoryStatus();
                     break;
                case Constant.CHEAK_AND_REMOVE_OLD_BOOK:
                    checkRemoveBook();
                     break;
             }
         }else if(realUser instanceof NormalUser){
            switch(choice){
                case Constant.SEARCH_BOOK:
                   searchBook();break;
                case Constant.DISPLAY_BOOK:
                    displayBook();break;
                case Constant.SYSTEM_EXIT:
                    systemExit();break;
                case Constant.BORROWED_BOOK:
                    borrowed();break;
                case Constant.RETURN_BOOK:
                    returnBook();break;
                case Constant.VIEW_HISTORY_BORROWED_BOOK:
                   viewBorrowedBook();
            }
        }
    }

    //普通用户
    public void displayBook(){
        if(realUser instanceof Administrator)
        ((Administrator)realUser).displayBook();
        else
            ((NormalUser)realUser).displayBook();
    }
    //借阅图书
    public void borrowed(){
        ((NormalUser)realUser).borrowed();
    }
    //归还图书
    public void returnBook(){
        ((NormalUser)realUser).returnBook();
    }
    //退出系统
    public void systemExit(){
        if(realUser instanceof Administrator)
            ((Administrator)realUser).systemExit();
        else
            ((NormalUser)realUser).systemExit();
    }
    //查看当前个人借阅情况
    public void viewBorrowedBook(){
        ((NormalUser)realUser).viewBorrowedBook();
    }

    //管理用户
    //上架图书
    public void add() {
        checkRealUserWhetherAdminUser("普通用户没有权限增加书籍！！！");
        ((Administrator)realUser).add();
    }


    //搜索图书
    public void searchBook(){
        if(realUser instanceof Administrator)
            ((Administrator)realUser).searchBook();
        else
            ((NormalUser)realUser).searchBook();

    }
    //修改图书
    public void updataBook(){
        checkRealUserWhetherAdminUser("普通用户不可以更新书籍信息！");
        ((Administrator)realUser).updataBook();
    }

    //下架图书
    public void removeBook(){
        checkRealUserWhetherAdminUser("普通用户没有权限删除！");
        ((Administrator)realUser).removeBook();
    }

    //统计借阅次数
    public void borrowedCount(){
        ((Administrator)realUser).borrowedCount();
    }

    //最受欢迎的图书
    public  void generateBook(){
       checkRealUserWhetherAdminUser("普通用户没有权限调用此功能！");
        ((Administrator)realUser).generateBook();
    }

    //查看库存状态
    public void checkInventoryStatus(){
        checkRealUserWhetherAdminUser("普通用户没有权限查看库存！");
        ((Administrator)realUser).checkInventoryStatus();
    };

    //检查上架一年为下架的图书
    public void checkRemoveBook(){
        ( (Administrator)realUser).checkRemoveBook();
    }
}
